package android.explorer.activity;
import java.io.File;

import myultility.Convert;
import myultility.Exec;
import android.app.Activity;
import android.content.Intent;
import android.explorer.R;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class FolderProperties extends Activity{
	TextView tvSize,tvContaint;
	public long filesCount=0,foldersCount=0,sizeTotal=0;
	public boolean success=true;
	String permission="rwxrwxrwxrwx",filename;
	Thread fc;
	Button bt;
	private String get(boolean b){
		return b?"Yes":"No";
	}
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.folderproperties);
			filename = this.getIntent().getExtras().getString("foldername");
			File file=new File( filename );
			setTitle(file.getName()+" properties");
			bt=(Button)findViewById(R.id.folderpropertiesbutton);
			((TextView)findViewById(R.id.folderpropertiesname)).setText("Folder name : "+file.getName());
			((TextView)findViewById(R.id.folderpropertiesparent)).setText("Parent : "+file.getParent());
			((TextView)findViewById(R.id.folderpropertiesishidden)).setText("Is hidden : "+get(file.isHidden()));
			(tvSize=(TextView)findViewById(R.id.folderpropertiessize)).setText("Total size : "+file.length()+" bytes");
			(tvContaint=(TextView)findViewById(R.id.folderpropertiescontaint)).setText("Containts : "+file.length()+" bytes");
			((ImageView)findViewById(R.id.folderpropertiesimage)).setImageResource(R.drawable.folder32);
			
			Exec exe;
			exe=new Exec(new String[] {"ls","-l","-d",file.getAbsolutePath()});
			
			
			if(exe.exe(100)){
				((TextView)findViewById(R.id.folderproperties_owner)).setText("Owner : "+Convert.toPermission(exe.result.substring(1,4)));
				((TextView)findViewById(R.id.folderproperties_group)).setText("Group owner : "+Convert.toPermission(exe.result.substring(4, 7)));
				((TextView)findViewById(R.id.folderproperties_everyone)).setText("Other : "+Convert.toPermission(exe.result.substring(7, 10)));
				permission=exe.result.substring(1, 10);
				Log.v("debug", "permission " + permission);
			}else{
				this.setTitle(exe.error);
			}
			fc=new FolderContaint(this,file);
			fc.start();
			fc.join();
		}catch (Exception e) {
			System.err.println(e);
		}
	}
	public void onOkButtonClick(View target) {
		try {
			fc.stop();
		} catch (Exception e) {
		}
		finish();
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		finish();
	}
	public void onChangeButtonClick(View target) {
		Intent intent=new Intent(this,android.explorer.activity.ChangePermission.class);
		intent.putExtra("filename",filename);
		intent.putExtra("permission", permission);
		startActivityForResult(intent,0);
	}
	public void set(){
		try {
			if(success){
				tvSize.setText("Total size : "+sizeTotal+" bytes");
				tvContaint.setText("Containts : "+foldersCount+" folder(s), "+filesCount+" file(s)");
			}else{
				tvSize.setText("Total size : more than "+sizeTotal+" bytes");
				tvContaint.setText("Containts : more than "+foldersCount+" folder(s) and "+filesCount+" file(s)");
			}
		} catch (Exception e) {
		}
	}
}
class FolderContaint extends Thread{
	File file;
	FolderProperties fp;
	public FolderContaint(FolderProperties fp,File file){
		this.file=file;
		this.fp=fp;
	}
	public void run(){
		fp.bt.setText("Counting...");
		dfs(file);
		fp.bt.setText("Close");
		fp.set();
	}
	private void dfs(File file){
		try{
			String strs[]=file.list();
			if(strs==null)return;
			for(String str:strs){
				try {
					File f=new File(file.getAbsolutePath()+"/"+str);
					if(!f.exists())return;
					if(f.isFile()){
						fp.sizeTotal+=f.length();
						fp.filesCount++;
					}else{
						fp.foldersCount++;
						if(f.list()!=null)
							dfs(f);
					}
				}catch(Exception e) {
				}
				if(fp.filesCount+fp.foldersCount>500){
					fp.success=false;
					return;
				}
			}
		}catch(Exception e){}
		fp.set();
	}
}

